/*
 * SQLNoticiaDAOFactory.java
 *
 * Created on 31 de octubre de 2007, 16:26
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package webportlet.model.noticia.dao;

/**/
import java.sql.Connection;
import java.util.ArrayList;
import javax.sql.DataSource;

import webportlet.model.noticia.dto.*;
import webportlet.model.noticia.dao.*;
import webportlet.model.util.*;
/**
 *
 * @author Administrador
 */
public class SQLNoticiaDAOFactory {
    
   private final static Class daoClass = getDAOClass();
   
   private SQLNoticiaDAOFactory(){}
   
   private static Class getDAOClass()
   {
       Class theClass = null;
       try
       {
           String daoClassName = "webportlet.model.noticia.dao.CCSQLNoticiaDAO";
           theClass = Class.forName(daoClassName);
           
       }catch(Exception e){
            e.printStackTrace();
        }
        return theClass;
   
   }
   public static SQLNoticiaDAO getDAO() throws InternalErrorException {
        
       try {        
            return (SQLNoticiaDAO) daoClass.newInstance();
        } catch (Exception e) {
            throw new InternalErrorException(e);
        }
    }
   
   public static void main (String[] args)
   {
       Connection connection = null;                                             
                                                                                  
        try {                                                                     
                                                                                  
            /* Get a connection. */                                               
            DataSource dataSource = new SimpleDataSource();                       
            connection = dataSource.getConnection();                              
                                                                                              /* Get dao. */                                                        //
            SQLNoticiaDAO dao = SQLNoticiaDAOFactory.getDAO();            
                                                                                  
            /* Test "SQLUserProfileDAO::create". */                               
            System.out.println("Test for 'SQLUNoticiaDAO::create'");           
            NoticiaDTO noticiaDTO = new NoticiaDTO(new Long(0), "Bellas Permuy","fbellas@udc.es", "gl");                                
                                        
            noticiaDTO = dao.create(connection, noticiaDTO);                               
            System.out.println("noticiaDTO created => " + noticiaDTO);   
            Long id = noticiaDTO.getId();
                                                                                 
            /* Test "SQLUserProfileDAO::exists". */                              
            System.out.println("Test for 'SQLUnoticiaDAO::exists'");           
           if (!dao.exits(connection,id)) {         
                throw new Exception("Can not find user profile with " +           
                    "Id = " +                                              
                    noticiaDTO.getId());                               
            }                                                            
                                                                            
            /* Test "SQLUserProfileDAO::find". */                           
            System.out.println("Test for 'SQLNoticiaDAO::find'");           
            System.out.println(dao.find(connection,noticiaDTO.getId()));         
            /* Test "SQLUserProfileDAO::update". */                    
            System.out.println("Test for 'SQLUserProfileDAO::update'");           
            
            noticiaDTO.setGenero("Smith");                            
            noticiaDTO.setTitulo("jsmith@acme.com");                    
            noticiaDTO.setDescripcion("en");                              
                               //
            dao.update(connection, noticiaDTO);                
            System.out.println(dao.find(connection, noticiaDTO.getId()));                                 
                                                                                  
            /* Test "SQLUserProfileDAO::remove". */                               
            System.out.println("Test for 'SQLUserProfileDAO::remove'"); 
            dao.remove(connection, noticiaDTO.getId());                                                                                                  //
           if (dao.exits(connection,id)) {          
                throw new Exception("SQLUserProfileDAO::remove failed");          //
            }  
            /* Para hacer este test es necesario haber ejecutado los scripts
             * de creacion de base de datos.
             * Test "SQLNoticiaDAO:recuperarlista". */
            System.out.println("Test for 'SQLNoticiaDAO::recuperarlista'");
            ArrayList <Long> listaIds = new ArrayList();
            listaIds.add(new Long(1));
            listaIds.add(new Long(2));
            listaIds.add(new Long(3));
            ArrayList <NoticiaDTO> listaDTOs = new ArrayList();
            listaDTOs = dao.recuperarLista(connection,listaIds);
            for(int i=0; i<listaDTOs.size();i++){
                System.out.println(listaDTOs.get(i));
            }
                                                                                  //
            /* Tests OK. */                                                       //
            System.out.println("Tests OK !!!!");                                  //
                                                                                  //
        } catch (Exception e) {                                                   //
            e.printStackTrace();                                                  //
        } finally {                                                               //
            try {                                                                 //
                GeneralOperations.closeConnection(connection);                    //
            } catch (InternalErrorException e) {                                  //
                e.printStackTrace();                                              //
            }                                                                     //
        }                                                                         //
                                     
      }
   }

